<?php

namespace PHPPgAdmin\XHtml;

use \PHPPgAdmin\Decorators\Decorator;

/**
 * Base HTMLController controller class
 */
class HTMLController
{
    use \PHPPgAdmin\HelperTrait;

    private $container      = null;
    private $data           = null;
    private $database       = null;
    private $server_id      = null;
    public $form            = '';
    public $href            = '';
    public $lang            = [];
    public $action          = '';
    public $_name           = 'HTMLController';
    public $controller_name = 'HTMLController';
    public $_title          = 'base';

    /* Constructor */
    public function __construct(\Slim\Container $container, $controller_name = null)
    {
        $this->container      = $container;
        $this->lang           = $container->get('lang');
        $this->view           = $container->get('view');
        $this->plugin_manager = $container->get('plugin_manager');
        $this->appName        = $container->get('settings')['appName'];
        $this->appVersion     = $container->get('settings')['appVersion'];
        $this->appLangFiles   = $container->get('appLangFiles');
        $this->misc           = $container->get('misc');
        $this->conf           = $this->misc->getConf();
        $this->appThemes      = $container->get('appThemes');
        $this->action         = $container->get('action');

        if ($controller_name !== null) {
            $this->controller_name = $controller_name;
        }

        //\PC::debug($this->_name, 'instanced controller');
    }

    public function getContainer()
    {
        return $this->container;
    }

    /**
     * Returns URL given an action associative array.
     * NOTE: this function does not html-escape, only url-escape
     * @param $action An associative array of the follow properties:
     *            'url'  => The first part of the URL (before the ?)
     *            'urlvars' => Associative array of (URL variable => field name)
     *                        these are appended to the URL
     * @param $fields Field data from which 'urlfield' and 'vars' are obtained.
     */
    protected function getActionUrl(&$action, &$fields, $from = null)
    {

        if ($from === null) {
            $from = __METHOD__;
        }

        $url = Decorator::get_sanitized_value($action['url'], $fields);

        if ($url === false) {
            return '';
        }

        if (!empty($action['urlvars'])) {
            $urlvars = Decorator::get_sanitized_value($action['urlvars'], $fields);
        } else {
            $urlvars = [];
        }

        /* set server, database and schema parameter if not presents */
        if (isset($urlvars['subject'])) {
            $subject = Decorator::get_sanitized_value($urlvars['subject'], $fields);
        } else {
            $subject = '';
        }

        if (isset($_REQUEST['server']) and !isset($urlvars['server']) and $subject != 'root') {
            $urlvars['server'] = $_REQUEST['server'];
            if (isset($_REQUEST['database']) and !isset($urlvars['database']) and $subject != 'server') {
                $urlvars['database'] = $_REQUEST['database'];
                if (isset($_REQUEST['schema']) and !isset($urlvars['schema']) and $subject != 'database') {
                    $urlvars['schema'] = $_REQUEST['schema'];
                }
            }
        }

        $sep = '?';

        ksort($urlvars);
        foreach ($urlvars as $var => $varfield) {
            $url .= $sep . Decorator::value_url($var, $fields) . '=' . Decorator::value_url($varfield, $fields);
            $sep = '&';
        }

        /*if (strpos($url, SUBFOLDER) === false) {
        $url = str_replace('//', '/', SUBFOLDER . '/' . $url);
        }*/
        //$this->prtrace('getActionUrl', $url, 'from', $from);

        return $url;
    }

    /**
     * Display a link
     * @param $link An associative array of link parameters to print
     *     link = array(
     *       'attr' => array( // list of A tag attribute
     *          'attrname' => attribute value
     *          ...
     *       ),
     *       'content' => The link text
     *       'fields' => (optionnal) the data from which content and attr's values are obtained
     *     );
     *   the special attribute 'href' might be a string or an array. If href is an array it
     *   will be generated by getActionUrl. See getActionUrl comment for array format.
     */
    public function printLink($link, $do_print = true, $from = null)
    {

        if (!isset($link['fields'])) {
            $link['fields'] = $_REQUEST;
        }
        if ($from === null || $from === false) {
            $from = __METHOD__;
        }

        $tag = '<a ';
        foreach ($link['attr'] as $attr => $value) {
            if ($attr == 'href' and is_array($value)) {
                $tag .= 'href="' . htmlentities($this->getActionUrl($value, $link['fields'], $from)) . '" ';
            } else {
                $tag .= htmlentities($attr) . '="' . Decorator::get_sanitized_value($value, $link['fields'], 'html') . '" ';
            }
        }
        $tag .= '>' . Decorator::get_sanitized_value($link['content'], $link['fields'], 'html') . "</a>\n";

        if ($do_print) {
            echo $tag;
        } else {
            return $tag;
        }
    }

    /**
     * Prints a combox box
     *
     * @param        $arrOptions associative array storing options and values of combo should be Option => Value
     * @param        $szName     string to specify the name of the form element
     * @param bool   $bBlankEntry
     * @param string $szDefault
     * @param bool   $bMultiple
     * @param int    $iSize
     * @return string with the generated HTML select box
     * @internal param $ (optional) $bBlankEntry bool to specify whether or not we want a blank selection
     * @internal param $ (optional) $szDefault string to specify the default VALUE selected
     * @internal param $ (optional) $bMultiple bool to specify whether or not we want a multi select combo box
     * @internal param $ (optional) $iSize int to specify the size IF a multi select combo
     */
    public static function printCombo(&$arrOptions, $szName, $bBlankEntry = true, $szDefault = '', $bMultiple = false, $iSize = 10)
    {
        $htmlOut = '';
        if ($bMultiple) // If multiple select combo
        {
            $htmlOut .= "<select rel=\"printCombo\" name=\"$szName\" id=\"$szName\" multiple=\"multiple\" size=\"$iSize\">" . "\n";
        } else {
            $htmlOut .= "<select rel=\"printCombo\" class=\"select2\" name=\"$szName\" id=\"$szName\">" . "\n";
        }

        if ($bBlankEntry) {
            $htmlOut .= "<option value=\"\"></option>\n";
        }

        foreach ($arrOptions as $curKey => $curVal) {
            $curVal = htmlspecialchars($curVal);
            $curKey = htmlspecialchars($curKey);
            if ($curVal == $szDefault) {
                $htmlOut .= "<option value=\"$curVal\" selected=\"selected\">$curKey</option>\n";
            } else {
                $htmlOut .= "<option value=\"$curVal\">$curKey</option>\n";
            }
        }
        $htmlOut .= "</select>\n";

        return $htmlOut;
    }

}
